﻿using QRCoder;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace ST.Common.Util.Drawing
{
	public class QRCodeHelper
	{
		#region 使用QRCoder生成二维码
		/// <summary>
		/// 生成二维码
		/// </summary>
		/// <param name="text"></param>
		/// <param name="width"></param>
		/// <param name="height"></param>
		/// <param name="fileFormat">默认PNG格式</param>
		/// <returns></returns>
		public static byte[] GenQRCode(string text, int width, int height, ImageFormat fileFormat)
		{
			byte[] bytes = null;
			QRCodeGenerator qrGenerator = new QRCodeGenerator();
			QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q);
			QRCode qrCode = new QRCode(qrCodeData);
			Bitmap qrCodeImage = qrCode.GetGraphic(20);//todo:不能自定义生成二维码的宽度和高度
			using (MemoryStream stream = new MemoryStream())
			{
				qrCodeImage.Save(stream, fileFormat);
				bytes = stream.ToArray();
			}
			if (qrCodeImage != null)
			{
				qrCodeImage.Dispose();
			}
			return bytes;
		}

		/// <summary>
		/// 生成PNG格式二维码
		/// </summary>
		/// <param name="text"></param>
		/// <param name="width"></param>
		/// <param name="height"></param>
		/// <returns></returns>
		public static byte[] GenQRCode(string text, int width, int height)
		{
			return GenQRCode(text, width, height, ImageFormat.Png);
		}

		/// <summary>
		/// 生成二维码中间带Logo
		/// </summary>
		/// <param name="title"></param>
		/// <param name="text"></param>
		/// <param name="logoFilePath"></param>
		/// <param name="width"></param>
		/// <param name="height"></param>
		/// <returns></returns>
		public static byte[] GenQRCodeWithLogo(string title, string text, string logoFilePath, int width, int height, ImageFormat fileFormat)
		{
			byte[] bytes = null;
			QRCodeGenerator qrGenerator = new QRCodeGenerator();
			QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q);
			QRCode qrCode = new QRCode(qrCodeData);
			Bitmap qrCodeImage = qrCode.GetGraphic(20, Color.Black, Color.White, (Bitmap)System.Drawing.Image.FromFile(logoFilePath));
			using (MemoryStream stream = new MemoryStream())
			{
				qrCodeImage.Save(stream, fileFormat);
				bytes = stream.ToArray();
			}
			if (qrCodeImage != null)
			{
				qrCodeImage.Dispose();
			}
			return bytes;
		}

		/// <summary>
		/// 生成PNG格式二维码中间带Logo
		/// </summary>
		/// <param name="title"></param>
		/// <param name="text"></param>
		/// <param name="logoFilePath"></param>
		/// <param name="width"></param>
		/// <param name="height"></param>
		/// <returns></returns>
		public static byte[] GenQRCodeWithLogo(string title, string text, string logoFilePath, int width, int height)
		{
			return GenQRCodeWithLogo(title, text, logoFilePath, width, height, ImageFormat.Png);
		}
		#endregion

		#region ZXing生成二维码
		//public static Bitmap GenQRCode(string text, int width, int height)
		//{
		//	try
		//	{
		//		QrCodeEncodingOptions options = new QrCodeEncodingOptions();
		//		options.Width = width;
		//		options.Height = height;
		//		options.CharacterSet = "UTF-8";
		//		options.DisableECI = true; //禁用ECI编码段: use UTF-8 encoding and the ECI segment is omitted
		//								   //options.ErrorCorrection = EsrrorCorrectionLevel.M;
		//		options.PureBarcode = true;//底部是否显示内容
		//								   //options.Margin = 10;//设置间隙
		//		BarcodeWriter writer = new BarcodeWriter();
		//		writer.Format = BarcodeFormat.QR_CODE;
		//		writer.Options = options;
		//		Bitmap bmp = writer.Write(text);
		//		return bmp;
		//	}
		//	catch (Exception ex)
		//	{
		//		log4net.LogManager.GetLogger(typeof(QRCodeHelper)).Error(ex);
		//	}
		//	return null;
		//}

		//public static bool GenQRCode(string text, int width, int height, string fileName, ImageFormat fileFormat)
		//{
		//	try
		//	{
		//		Bitmap bmp = GenQRCode(text, width, height);
		//		if (bmp != null)
		//		{
		//			bmp.Save(fileName, fileFormat);
		//			return true;
		//		}
		//	}
		//	catch (Exception ex)
		//	{
		//		log4net.LogManager.GetLogger(typeof(QRCodeHelper)).Error(ex);
		//	}
		//	return false;
		//}

		//public static Bitmap GenQRCodeWithLogo(string title, string text, string logoFilePath, int width, int height)
		//{
		//	try
		//	{
		//		Image imgLogo = Bitmap.FromFile(logoFilePath);

		//		MultiFormatWriter mutiWriter = new MultiFormatWriter();
		//		Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
		//		hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
		//		hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

		//		//生成二维码
		//		BitMatrix bm = mutiWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hint);
		//		BarcodeWriter barcodeWriter = new BarcodeWriter();
		//		Bitmap bitmap = barcodeWriter.Write(bm);

		//		//获取二维码实际尺寸（去掉二维码两边空白后的实际尺寸）
		//		int[] rectangle = bm.getEnclosingRectangle();

		//		//计算插入图片的大小和位置
		//		int middleImgW = imgLogo.Width;
		//		int middleImgH = imgLogo.Height;
		//		int middleImgL = (bitmap.Width - middleImgW) / 2;
		//		int middleImgT = (bitmap.Height - middleImgH) / 2;

		//		int titleHeight = 30;//文字高度30
		//							 //将img转换成bmp格式，否则后面无法创建 Graphics对象
		//		Bitmap bmp = null;
		//		if (!string.IsNullOrEmpty(title))
		//		{
		//			middleImgT = middleImgT + titleHeight;
		//			bmp = new Bitmap(bitmap.Width, bitmap.Height + titleHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
		//		}
		//		else
		//		{
		//			bmp = new Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
		//		}
		//		using (Graphics g = Graphics.FromImage(bmp))
		//		{
		//			g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
		//			g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
		//			g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
		//			g.DrawImage(bitmap, 0, 0);
		//		}
		//		//在二维码中插入图片
		//		Graphics myGraphic = Graphics.FromImage(bmp);
		//		if (!string.IsNullOrEmpty(title))
		//		{
		//			//白底
		//			myGraphic.FillRectangle(Brushes.White, middleImgL - 3, middleImgT - 3 - titleHeight, middleImgW + 6, middleImgH + 6);
		//			myGraphic.DrawImage(imgLogo, middleImgL, middleImgT - titleHeight, middleImgW, middleImgH);
		//			StringFormat format = new StringFormat();
		//			format.Alignment = StringAlignment.Center;
		//			myGraphic.DrawString(title, new Font("Microsoft Yahei", 16), new SolidBrush(Color.Red), new Rectangle(3, height - titleHeight - 3, width, titleHeight), format);
		//		}
		//		else
		//		{
		//			//白底
		//			myGraphic.FillRectangle(Brushes.White, middleImgL - 3, middleImgT - 3, middleImgW + 6, middleImgH + 6);
		//			myGraphic.DrawImage(imgLogo, middleImgL, middleImgT, middleImgW, middleImgH);
		//		}
		//		imgLogo.Dispose();
		//		return bmp;
		//	}
		//	catch (Exception ex)
		//	{
		//		log4net.LogManager.GetLogger(typeof(QRCodeHelper)).Error(ex);
		//	}
		//	return null;
		//}

		//public static bool GenQRCodeWithLogo(string title, string text, string logoFilePath, int width, int height, string fileName, ImageFormat fileFormat)
		//{
		//	try
		//	{
		//		Bitmap bmp = GenQRCodeWithLogo(title, text, logoFilePath, width, height);
		//		if (bmp != null)
		//		{
		//			bmp.Save(fileName, fileFormat);
		//			return true;
		//		}
		//	}
		//	catch (Exception ex)
		//	{
		//		log4net.LogManager.GetLogger(typeof(QRCodeHelper)).Error(ex);
		//	}
		//	return false;
		//}
		#endregion
	}
}
